package com.znufe.exam.controller.vote;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.znufe.exam.base.BaseApiController;
import com.znufe.exam.domain.students.StudentVoteInfoObject;
import com.znufe.exam.service.StudentService;
import com.znufe.exam.viewmodel.vote.signin.ChangeSignInStatusRequestVM;
import com.znufe.exam.viewmodel.vote.signin.InitializeSignInInfoRequestVM;
import com.znufe.exam.viewmodel.vote.signin.SignInCreateVM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.znufe.exam.domain.vote.SignIn;
import com.znufe.exam.service.SignInService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.znufe.exam.base.RestResponse;
import com.znufe.exam.base.SystemCode;

/**   
 * 
 * @Description:  学生签到信息类接口层
 * @Author:       bot   
 * @CreateDate:   SignInfo
 * @Version:      V1.0
 *    
 */
@Api(description = "学生签到信息类",value="学生签到信息类" )
@RestController
@RequestMapping("/api/signIn")
public class SignInController extends BaseApiController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	public SignInService signInService;
	@Autowired
    private StudentService studentService;


	@RequestMapping(value="/changeSignInStatus", method = RequestMethod.POST,
			        produces = "application/json")
	@ApiOperation(value = "某堂课某个学生的签到状态改变接口（未签到/已签到）",
			      notes = "改变某堂课某个学生的签到状态[sign_in]，作者：lin keqing")
	public RestResponse<Integer> changeStatus(@RequestBody ChangeSignInStatusRequestVM model) {
		if (null == model.getClassId() || null == model.getStuId()) {
			return new RestResponse<>(2, "班级id与学生id均不能为空，请重试");
		}
		SignIn signIn = signInService.selectByStuIdAndClassId(model);
		Integer newStatus = signIn.getIsSignIn() == 0 ? 1 : 0;
		signIn.setIsSignIn(newStatus);
		signIn.setModifyTime(new Date());
		signInService.updateByIdFilter(signIn);
		return RestResponse.ok(newStatus);
	}


	@RequestMapping(value="/initialize_students_sign_in_info", method = RequestMethod.POST,
			produces = "application/json")
	@ApiOperation(value = "初始化某个班、某堂课的学生签到信息",
			notes = "在某个班准备上某个课程之前，在sign_in表中新增相关信息[sign_in]，作者：lin keqing")
	public RestResponse<List<SignInCreateVM>> initializeStudentsSignInInfo(@RequestBody InitializeSignInInfoRequestVM model) {
        // 拿出这个班上所有学生的答题器编号、基站编号信息
	    List<StudentVoteInfoObject> voteInfoOfClassStudents = studentService
                .selectAllStudentVoteInfoInClass(model.getClassId());
        // 将上面拿到的信息注入到SignInCreateVM的List中
        List<SignInCreateVM> toInitializeSignInInfos = voteInfoOfClassStudents.stream().map(s -> {
            SignInCreateVM vm = modelMapper.map(s, SignInCreateVM.class);
            vm.setClassId(model.getClassId());
            vm.setCourseId(model.getCourseId());
            return vm;
        }).collect(Collectors.toList());
        // 进行批量的插入
        signInService.insertBatchSignInInfo(toInitializeSignInInfos);
	    return RestResponse.ok(toInitializeSignInInfos);
	}

	/**
	 * @explain 查询学生签到信息类对象  <swagger GET请求>
	 * @param   id
	 * @return  signIn
	 * @author  bot
	 * @time    SignInfo
	 */
	/*
	@GetMapping("/getSignInById/{id}")
	@ApiOperation(value = "获取学生签到信息类信息", notes = "获取学生签到信息类信息[signIn]，作者：bot")
	@ApiImplicitParam(paramType="path", name = "id", value = "学生签到信息类id", required = true, dataType = "Integer")
	public RestResponse<SignIn> getSignInById(@PathVariable("id")Integer id){

		try {
			SignIn signIn=signInService.selectByPrimaryKey(id);
			if (signIn != null) {
				return RestResponse.ok(signIn);
			} else {
				logger.error("查询学生签到信息类执行失败："+signIn.toString());
				return RestResponse.fail(SystemCode.NoDataExist.getCode(),SystemCode.NoDataExist.getMessage());
			}
		} catch (Exception e) {
			logger.error("查询学生签到信息类执行异常："+e.getMessage());
			return RestResponse.fail(SystemCode.InnerError.getCode(),SystemCode.InnerError.getMessage());
		}
	}
	*/

	/**
	 * @explain 添加学生签到信息类对象
	 * @param   signIn
	 * @return  int
	 * @author  bot
	 * @time    SignInfo
	 */
	/*
	@PostMapping("/insertSelective")
	@ApiOperation(value = "添加学生签到信息类", notes = "添加学生签到信息类[signIn],作者：bot")
	public RestResponse<Integer> insertSelective(@RequestBody SignIn signIn){
		try {
			int rg=signInService.insertSelective(signIn);
			if (rg > 0) {
				return RestResponse.ok(rg);
			} else {
				logger.error("添加学生签到信息类执行失败："+signIn.toString());
				return RestResponse.fail(SystemCode.InsertDataFail.getCode(),SystemCode.InsertDataFail.getMessage());
			}
		} catch (Exception e) {
			logger.error("添加学生签到信息类执行异常："+e.getMessage());
			return RestResponse.fail(SystemCode.InnerError.getCode(),SystemCode.InnerError.getMessage());
		}
	}
	*/

	/**
	 * @explain 删除学生签到信息类对象
	 * @param   id
	 * @return  int
	 * @author  bot
	 * @time    SignInfo
	 */
	/*
	@PostMapping("/deleteByPrimaryKey")
	@ApiOperation(value = "删除学生签到信息类", notes = "删除学生签到信息类,作者：bot")
	@ApiImplicitParam(paramType="query", name = "id", value = "学生签到信息类id", required = true, dataType = "Integer")
	public RestResponse<Integer> deleteByPrimaryKey(Integer id){
		try {
			int reg=signInService.deleteByPrimaryKey(id);
			if (reg > 0) {
				return RestResponse.ok(reg);
			} else {
				logger.error("删除学生签到信息类失败ID："+id);
				return RestResponse.fail(SystemCode.DeleteDataFail.getCode(),SystemCode.DeleteDataFail.getMessage());
			}
		} catch (Exception e) {
			logger.error("删除学生签到信息类执行异常："+e.getMessage());
			return RestResponse.fail(SystemCode.InnerError.getCode(),SystemCode.InnerError.getMessage());
		}
	}
	*/

	/**
	 * @explain 修改学生签到信息类对象
	 * @param   signIn
	 * @return  signIn
	 * @author  bot
	 * @time    SignInfo
	 */
	/*
	@ApiOperation(value = "修改学生签到信息类", notes = "修改学生签到信息类[signIn],作者：bot")
	@PostMapping("/updateByPrimaryKeySelective")
	public RestResponse<Integer> updateByPrimaryKeySelective(@RequestBody SignIn signIn){
		try {
			int reg = signInService.updateByPrimaryKeySelective(signIn);
			if (reg > 0) {
				return RestResponse.ok(reg);
			} else {
				logger.error("修改学生签到信息类失败ID："+signIn.toString());
				return RestResponse.fail(SystemCode.UpdateDataFail.getCode(),SystemCode.UpdateDataFail.getMessage());
			}
		} catch (Exception e) {
			logger.error("修改学生签到信息类执行异常："+e.getMessage());
			return RestResponse.fail(SystemCode.InnerError.getCode(),SystemCode.InnerError.getMessage());
		}
	}
	*/

	/**
	 * @explain 获取匹配学生签到信息类
	 * @param   signIn
	 * @return  List<SignIn>
	 * @author  bot
	 * @time    SignInfo
	 */
	/*
	@ApiOperation(value = "条件查询学生签到信息类", notes = "条件查询[signIn],作者：bot")
	@PostMapping("/querySignInList")
	public RestResponse<List<SignIn>> querySignInList(SignIn signIn){
		try {
			List<SignIn> list = signInService.querySignInList(signIn);
			return RestResponse.ok(list);
		} catch (Exception e) {
			logger.error("获取学生签到信息类执行异常："+e.getMessage());
			return RestResponse.fail(SystemCode.InnerError.getCode(),SystemCode.InnerError.getMessage());
		}
	}
    */
}